#include <iostream>
#include <bitset>
#define MAX_SITUATION_NUMBER 8
using namespace std;

bool check_safe(int location)
{
    // location：羊，白菜，狼。三位二进制数。
    // 羊和白菜
    if(location & 4 == location & 2)
    {
        return false;
    }
    // 羊和狼
    if(location & 4 == location & 1)
    {
        return false;
    }
    return true;
}

typedef struct Node
{
    int location;
    struct Node *top;
    struct Node *rear;
} Node, *Queue;

void Init(Queue L)
{
    L->rear = L;
    L->top = L;
}
int Remove(Node *p)
{
    p->top->rear = p->rear;
    p->rear->top = p->top;
    int character = p->location;
    free(p);
    return character;
}

void Append(Queue &L, int location)
{
    //NodeList p = (NodeList)malloc(sizeof(Node));
    Node *p = (Node *)malloc(sizeof(Node));
    p->location = location;
    p->top = L->top;
    p->rear = L;
    L->top->rear = p;
    L->top = p;
}

int Pop(Queue &L)
{
    return Remove(L->top);
}

void InQueue(Queue &Q, int location)
{
    Append(Q, location);
}

int OutQueue(Queue &Q)
{
    return Remove(Q->rear);
}

int Empty(Queue &Q)
{
    if (Q->rear == Q && Q->top == Q)
    {
        return 1;
    }
    return 0;
}

int main()
{
    Queue bfs = (Queue)malloc(sizeof(Node));
    Init(bfs);
    InQueue(bfs, 000);
    int path[MAX_SITUATION_NUMBER];
    for(int i=0; i<MAX_SITUATION_NUMBER; i++)
    {
        path[i] = -1;
    }
    path[0] = 0;
    int location, newLocation;
    while(!Empty(bfs) && path[MAX_SITUATION_NUMBER -1] == -1)
    {
        location = OutQueue(bfs);
        for(int target = 1; target < MAX_SITUATION_NUMBER; target <<= 1)
        {
            newLocation = location ^ target;
            if(path[newLocation] == -1 && check_safe(newLocation))
            {
                InQueue(bfs, newLocation);
                path[newLocation] = location;
            }
        }
    }
    if(path[MAX_SITUATION_NUMBER -1] == -1)
    {
        cout << "No Such Path" << endl;
    }
    else
    {
        int now = 7;
        int pre = path[MAX_SITUATION_NUMBER -1];
        do
        {
            cout << " < " << bitset<3>(now);
            now = pre;
            pre = path[now];
        } while (pre != now);
        cout << " < " << bitset<3>(now);
    }
    return 0;
}
